import { message } from 'ant-design-vue'

export const result = (response, authShowErrorMsg = true) => {
  const { data } = response
  if (data.code === 400 && authShowErrorMsg) {
    message.error(data.msg)
  }
  // if (data.code === 200) {
  //   message.success(data.msg)
  // }
  return {
    success(cb) {
      if (data.code !== 400) {
        cb(data, response)
      }
      return this
    },
    fail(cb) {
      if (data.code === 400) {
        cb(data, response)
      }
      return this
    },
    finally(cb) {
      cb(data, response)
      return this
    },
  }
}

export const clone = (obj) => {
  return JSON.parse(JSON.stringify(obj))
}

const tsPadStart = (str) => {
  str = String(str)

  return str.padStart(2, '0')
}

export const formatTimeStamp = (ts) => {
  const date = new Date(Number(ts))

  const YYYY = date.getFullYear()
  const MM = tsPadStart(date.getMonth() + 1)
  const DD = tsPadStart(date.getDate())

  const hh = tsPadStart(date.getHours())
  const mm = tsPadStart(date.getMinutes())
  const ss = tsPadStart(date.getSeconds())

  return `${YYYY}-${MM}-${DD} ${hh}:${mm}:${ss}`
}

export const daysBetweenTimestamps = (timestamp1, timestamp2) => {
  // 将时间戳转换为毫秒
  const ms1 = timestamp1 
  const ms2 = timestamp2 

  // 计算时间戳之间的差值
  const differenceInMilliseconds = Math.abs(ms1 - ms2)

  // 将毫秒差值转换为天数
  const days = differenceInMilliseconds / (1000 * 60 * 60 * 24)

  // 返回天数，四舍五入到最接近的整数
  return Math.round(days)
}

// 示例用法：
// const timestamp1 = 1648000000; // 2022-02-22 00:00:00
// const timestamp2 = 1648086400; // 2023-02-23 00:00:00
// console.log(daysBetweenTimestamps(timestamp1, timestamp2)); // 输出：1

export const convertToTimestamp=(dateTimeStr)=> {
  const date = new Date(dateTimeStr)
  return date.getTime()
}

// 使用示例
// const formattedTime = '2023-04-18 13:14:23'
// const timestamp = convertToTimestamp(formattedTime)
// console.log(timestamp)
